<?php
/**
 * Created by PhpStorm.
 * User: 2016-08-23
 * Date: 2017/4/6
 * Time: 15:11
 */
class ScsOperationalController extends BaseController {
    /**
     * @Description:运营看板关于金额的数据
     */
    public function getMoneyData(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $shop_id = Input::get('shop_id');
            if(!$shop_id){
                $response = Lang::get("scs_error.args_error_10000");
                break;
            }
            if(Input::get('data_type') && Input::get('data_type') == 'month'){
                $this_b_time = date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
                $this_e_time = date("Y-m-d 23:59:59", time());
                $last_b_time = date('Y-m-01 00:00:00', strtotime('-1 year'));
                $last_e_time = date('Y-m-t 23:59:59', strtotime('-1 year'));
            }else{
                $this_b_time = date("Y-m-d 00:00:00", time());
                $this_e_time = date("Y-m-d 23:59:59", time());
                $last_b_time = date("Y-m-d 00:00:00", strtotime("-1 day"));
                $last_e_time = date("Y-m-d 23:59:59", strtotime("-1 day"));
            }
            // $this_b_time = "2017-03-31 00:00:00";
            // $this_e_time = "2017-03-31 23:59:59";
            // $last_b_time = "2017-03-30 00:00:00";
            // $last_e_time = "2017-03-30 23:59:59";
            $this_money_tolal = ScsOperational::executeMoneySql($shop_id,ScsOperational::getTimeDiff($this_b_time),ScsOperational::getTimeDiff($this_e_time));
            $last_money_tolal = ScsOperational::executeMoneySql($shop_id,ScsOperational::getTimeDiff($last_b_time),ScsOperational::getTimeDiff($last_e_time));
            $ratio = 0;
            if($last_money_tolal != 0){
                $ratio = ($this_money_tolal - $last_money_tolal) / $last_money_tolal;
            }
            //总额
            $total = array('this_tolal'=>$this_money_tolal / 100,'last_tolal'=>$last_money_tolal / 100,'ratio'=>bcmul($ratio,100,2));
            //支付方式
            $result = ScsOperational::executePayTypeSql($shop_id,ScsOperational::getTimeDiff($this_b_time),ScsOperational::getTimeDiff($this_e_time));
            $pay_arr = ScsOperational::getPayName($result);
            $response['result']['total']=$total;
            $pay_arr = ScsOperational::sortArrByOneField($pay_arr,'money',true);
            $response['result']['pay_type_array'] = array_slice($pay_arr,0,5);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getMoneyData", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Description:运营看板关于分类的数据
     */
    public function getClassifyData(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $shop_id = Input::get('shop_id');
            if(!$shop_id){
                $response = Lang::get("scs_error.args_error_10000");
                break;
            }
            $service_count = array();
            $id_arr = array();
            $class_one = ServiceClassification::where('status',1)->where('level',1)->select('id','parent_id')->get();
            foreach($class_one as $value){
                $class_two = ServiceClassification::where('status',1)->where('level',2)->where('parent_id',$value->id)->select('id')->get();
                foreach ($class_two as $value1){
                    $class_three = ServiceClassification::where('status',1)->where('level',3)->where('parent_id',$value1->id)->select('id')->get();
                    foreach($class_three as $value2){
                        $id_arr[$value->id][] = $value2->id;
                    }
                }
            }
            if(Input::get('data_type') && Input::get('data_type') == 'month'){
                $this_b_time = date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
                $this_e_time = date("Y-m-d 23:59:59", time());
            }else{
                $this_b_time = date("Y-m-d 00:00:00", time());
                $this_e_time = date("Y-m-d 23:59:59", time());
            }
            // $this_b_time = "2017-03-31 00:00:00";
            // $this_e_time = "2017-03-31 23:59:59";
            $orders = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
                ->whereBetween('pay_time',array(ScsOperational::getTimeDiff($this_b_time),ScsOperational::getTimeDiff($this_e_time)))
                ->where('shop_id',$shop_id)
                ->where('place',2)
                ->get();
            foreach ($orders as $_order){
                $order_product = OrderProduct::where('order_id',$_order->order_id)
                    ->where('status',1)
                    ->get();
                foreach ($order_product as $_order_product) {
                    if ($_order_product->product_type == 'enterprise') {//说明是 企业报
                        $opep=OrderProductEsPackage::where("order_product_id",$_order_product->id)->get();
                        foreach ($opep as $_opep) {
                            $propack = ProductPackage::find($_opep->package_id);
                            if($propack){
                                foreach ($id_arr as $key=>$va){
                                    if(in_array($propack->classify,$va)){
                                        if(!isset($service_count[$key])){
                                            $service_count[$key] = array('id'=>$key);
                                        }
                                        if(!isset($service_count[$key]['order_ids']) || !in_array($_order->order_id,$service_count[$key]['order_ids']) ){
                                            $service_count[$key]['order_ids'][] = $_order->order_id;
                                            if(isset($service_count[$key]['count'])){
                                                $service_count[$key]['count'] += 1;
                                            }else{
                                                $service_count[$key]['count'] = 1;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }else{
                        $propack=ProductPackage::find($_order_product->product_id);
                        if($propack){
                            foreach ($id_arr as $key=>$va){
                                if(in_array($propack->classify,$va)){
                                    if(!isset($service_count[$key])){
                                        $service_count[$key] = array('id'=>$key);
                                    }
                                    if(!isset($service_count[$key]['order_ids']) || !in_array($_order->order_id,$service_count[$key]['order_ids']) ){
                                        $service_count[$key]['order_ids'][] = $_order->order_id;
                                        if(isset($service_count[$key]['count'])){
                                            $service_count[$key]['count'] += 1;
                                        }else{
                                            $service_count[$key]['count'] = 1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $service_count = ScsOperational::getServiceName($service_count);
            $service_count = ScsOperational::getRatio($service_count);
            $service_count = ScsOperational::sortArrByOneField($service_count,'count',true);
            $response['result']['service_classify'] = array_slice($service_count,0,5);
            //耗材分类
            $supply_count = ScsOperational::getSupplys($shop_id,ScsOperational::getTimeDiff($this_b_time),ScsOperational::getTimeDiff($this_e_time));
            $supply_count = ScsOperational::getRatio($supply_count);
            $supply_count = ScsOperational::sortArrByOneField($supply_count,'count',true);
            $response['result']['supply_classify'] = array_slice($supply_count,0,5);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getClassifyData", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Description:运营看板关于评价的数据
     */
    public function getEvaluationData(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $shop_id = Input::get('shop_id');
            $result = array();
            if(Input::get('data_type') && Input::get('data_type') == 'month'){
                $this_b_time = date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
                $this_e_time = date("Y-m-d 23:59:59", time());
            }else{
                $this_b_time = date("Y-m-d 00:00:00", time());
                $this_e_time = date("Y-m-d 23:59:59", time());
            }
            // $this_b_time = "2017-03-23 00:00:00";
            // $this_e_time = "2017-03-23 23:59:59";
            $orders = Orders::where('shop_id',$shop_id)
                ->whereIn('status',array(1018,1017,1217,1317,1417))
                ->whereBetween('pay_time',array(ScsOperational::getTimeDiff($this_b_time),ScsOperational::getTimeDiff($this_e_time)))
                ->orderBy('pay_time','desc')
                ->get();
            foreach($orders as $or){
                $arr = OrderEvaluation::where('order_evaluation.order_id',$or->order_id)
                    ->leftJoin('orders',"order_evaluation.order_id","=","orders.order_id")
                    ->leftJoin('users',"users.id","=","orders.user_id")
                    ->leftJoin('maintenance_shops',"maintenance_shops.id","=","orders.shop_id");
                if(Input::get('level')) {
                    if (Input::get('level') == 1) {
                        $arr = $arr->where('order_evaluation.star', '>=', 3.5);
                    } else {
                        $arr = $arr->where('order_evaluation.star', '<', 3.5);
                    }
                }
                $arr = $arr->select("order_evaluation.*","orders.shop_id","maintenance_shops.name as shop_name","orders.user_id",
                    "users.phone","users.type","users.real_name")->first();
                if($arr) {
                    $arr = $arr->toArray();
                    $result[] = $arr;
                }
            }
            $result = ScsOperational::sortArrByOneField($result,'updated_at',true);
            $response['result'] = $result;
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getEvaluationData", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Description:运营看板关于用户的数据
     */
    public function getUserCountData(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $shop_id = Input::get('shop_id');
            if(!$shop_id){
                $response = Lang::get("scs_error.args_error_10000");
                break;
            }
            $result = array();
            if(Input::get('data_type') && Input::get('data_type') == 'month'){
                $this_b_time = date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
                $this_e_time = date("Y-m-d 23:59:59", time());
            }else{
                $this_b_time = date("Y-m-d 00:00:00", time());
                $this_e_time = date("Y-m-d 23:59:59", time());
            }
            // $this_b_time = "2017-03-31 00:00:00";
            // $this_e_time = "2017-03-31 23:59:59";
            $result = ScsOperational::getUserCount($result,$shop_id,ScsOperational::getTimeDiff($this_b_time),ScsOperational::getTimeDiff($this_e_time));
            $result = ScsOperational::getOrderCount($result,$shop_id,$this_b_time,$this_e_time);
            $response['result'] = $result;

        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getUserCountData", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Description:运营看板关于车的数据
     */
    public function getCarData(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $shop_id = Input::get('shop_id');
            if(!$shop_id){
                $response = Lang::get("scs_error.args_error_10000");
                break;
            }
            $result = array('new_ins'=>0,'old_ins'=>0);
            $order_ids = array();
            $order_ids_ch = array();
            if(Input::get('data_type') && Input::get('data_type') == 'month'){
                $this_b_time = date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
                $this_e_time = date("Y-m-d 23:59:59", time());
            }else{
                $this_b_time = date("Y-m-d 00:00:00", time());
                $this_e_time = date("Y-m-d 23:59:59", time());
            }
            // $this_b_time = "2017-03-31 00:00:00";
            // $this_e_time = "2017-03-31 23:59:59";
            $ins = Insurance::where('website',$shop_id)->whereBetween('created_at',array($this_b_time,$this_e_time))
                ->select('is_renewal_insurance')
                ->get();
            foreach($ins as $in){
                if($in->is_renewal_insurance == 0){
                    $result['new_ins'] += 1;
                }else{
                    $result['old_ins'] += 1;
                }
            }
            $orders = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
                ->whereBetween('pay_time',array(ScsOperational::getTimeDiff($this_b_time),ScsOperational::getTimeDiff($this_e_time)))
                ->where('shop_id',$shop_id)
                ->where('place',2)
                ->get();
            foreach ($orders as $_order){
                $user = Users::find($_order->user_id);
                if($user && $user->type == 2) {
                    continue;
                }
                $order_product = OrderProduct::where('order_id', $_order->order_id)
                    ->where('status', 1)
                    ->get();
                foreach ($order_product as $_order_product) {
                    if ($_order_product->product_type == 'enterprise') {//说明是 企业报
                        $opep = OrderProductEsPackage::where("order_product_id", $_order_product->id)->get();
                        foreach ($opep as $_opep) {
                            if (in_array($_opep->package_id, array(1, 2))) {
                                if (!in_array($_order->order_id, $order_ids)) {
                                    $order_ids[] = $_order->order_id;
                                }
                                break;
                            }
                        }
                    } else {
                        if (in_array($_order_product->product_id, array(1, 2))) {
                            if (!in_array($_order->order_id, $order_ids)) {
                                $order_ids[] = $_order->order_id;
                            }
                        }
                    }
                }
            }
            $result['should_check'] = sizeof($order_ids);
            foreach($order_ids as $ord){
                $de = OrderDetection::where('order_id',$ord)->get();
                if($de && !empty($de) && sizeof($de) > 0 && !in_array($ord, $order_ids_ch)){
                    $order_ids_ch[] = $ord;
                }
            }
            $result['had_check'] = sizeof($order_ids_ch);
            $result['check_ratio'] = 0;
            if($result['should_check'] != 0){
                $result['check_ratio'] = bcmul($result['had_check'] / $result['should_check'],100,2);
            }
            $result = ScsOperational::getCarStationCount($result,$shop_id,$this_b_time,$this_e_time);
            $response['result'] = $result;
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getCarData", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Description:运营看板 日报表
     */
    public function getDailyData(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
                'date' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $sc_user = $this->verificationSCSUserToken(Input::get("token"), $response);
            if (is_null($sc_user)) {
                break;
            }
            $shop_id = Input::get('shop_id')?Input::get('shop_id'):0;
            $date = Input::get('date');
//            $date = '2017-07-12';
            $cols = array(
                'received',//当日实收
                'packages',//套餐卡收入
                'payable',//当日应收
                'consume',//冲减预收
                'journal',//当日流水
                'scrap',//废品收入
                'received_debt',//收回欠款
                'cash_payable',//现金账户预收
                'ins_payable',//虚拟账户预收
                'external',//外部调出
            );
            $result = array(
                'date'=>$date
            );
            $time = array($date . " 00:00:00",$date . " 23:59:59");
//            $time = array("2017-07-12 00:00:00","2017-07-12 23:59:59");
            $pay_type = Orders::$paytype;
            unset($pay_type[0]);
//            unset($pay_type[9]);
            unset($pay_type[14]);
            $ke = 9;
            $va = $pay_type[$ke];
            unset($pay_type[$ke]);
            $pay_type[$ke] = $va;
            foreach($pay_type as $key=>$val){
                if($key != 6 && $key != 8){
                    if($key == 9){
                        $orders_1 = OrderPay::leftJoin('orders', 'order_pay_record.order_id', '=', 'orders.order_id')
                            ->where('orders.pay_type', $key)->where('order_pay_record.status', 1)->whereIn('orders.status', Orders::$complete);
                        if ($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                            $orders_1 = $orders_1->where('orders.shop_id', $shop_id);
                        }
                        $orders_1 = $orders_1->whereBetween('orders.pay_time', $time)->select('order_pay_record.pay_type', 'order_pay_record.amount')->get();
                        $result = ScsOperational::getMultipleAmount($orders_1,$result,'plus');
                        $orders_2 = CarWashOrderPayRecord::leftJoin('car_wash_order', 'car_wash_order_pay_record.order_id', '=', 'car_wash_order.order_id')
                            ->where('car_wash_order.pay_type', $key)->where('car_wash_order_pay_record.status', 1)->whereIn('car_wash_order.status', Orders::$complete);
                        if ($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                            $orders_2 = $orders_2->where('car_wash_order.shop_id', $shop_id);
                        }
                        $orders_2 = $orders_2->whereBetween('car_wash_order_pay_record.implement_time', $time)->select('car_wash_order_pay_record.pay_type', 'car_wash_order_pay_record.amount')->get();
                        $result = ScsOperational::getMultipleAmount($orders_2,$result,'plus');
                        $orders_3 = ReturnOrderRecord::LeftJoin('return_order', 'return_order_record.return_order_id', '=', 'return_order.id')
                            ->LeftJoin('orders', 'orders.order_id', '=', 'return_order.order_id');
                        if ($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                            $orders_3 = $orders_3->where('orders.shop_id', $shop_id);
                        }
                        $orders_3 = $orders_3->whereBetween('return_order_record.created_at', $time)->where('orders.pay_type', $key)
                            ->select('return_order_record.pay_type', 'return_order_record.amount')->get();
                        $result = ScsOperational::getMultipleAmount($orders_3,$result,'minus');
                        $orders_4 = ReturnWashOrderRecord::LeftJoin('return_carwash_order', 'return_washorder_record.return_washorder_id', '=', 'return_carwash_order.id')
                            ->LeftJoin('car_wash_order', 'car_wash_order.order_id', '=', 'return_carwash_order.order_id');
                        if ($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                            $orders_4 = $orders_4->where('car_wash_order.shop_id', $shop_id);
                        }
                        $orders_4 = $orders_4->whereBetween('return_washorder_record.created_at', $time)->where('car_wash_order.pay_type', $key)
                            ->select('return_washorder_record.pay_type', 'return_washorder_record.amount')->get();
                        $result = ScsOperational::getMultipleAmount($orders_4,$result,'minus');
                       // $orders_5 = ReceivablePay::LeftJoin('receivable','receivable_pay_record.receivable_id','=','receivable.receivable_id');
                       // if($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                       //     $orders_5 = $orders_5->where('receivable.shop_id', $shop_id);
                       // }
                       // $orders_5 = $orders_5->whereBetween('receivable.verify_time',$time)->where('receivable.pay_type',$key)
                       //     ->select('receivable_pay_record.pay_type','receivable_pay_record.amount')->get();
                       // foreach($orders_5 as $o5){
                       //     if(isset($result['types'][$o5->pay_type]['received'])){
                       //         $result['types'][$o5->pay_type]['received_debt'] += $o5->amount;
                       //     }else{
                       //         $result['types'][$o5->pay_type]['received_debt'] = $o5->amount;
                       //     }
                       // }
                       // 
                        


                        $orders_6 = ServiceCardOrderPayRecord::leftJoin('service_card_order', 'service_card_order_pay_record.order_id', '=', 'service_card_order.order_id')
                            ->where('service_card_order.pay_type', $key)->where('service_card_order_pay_record.status', 1)->whereIn('service_card_order.status', Orders::$complete);
                        if ($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                            $orders_6 = $orders_6->where('service_card_order.shop_id', $shop_id);
                        }
                        $orders_6 = $orders_6->whereBetween('service_card_order_pay_record.implement_time', $time)->select('service_card_order_pay_record.pay_type', 'service_card_order_pay_record.amount')->get();
                        foreach ($orders_6 as $o6) {
                            if (isset($result['types'][$o6->pay_type]['packages'])) {
                                $result['types'][$o6->pay_type]['packages'] += $o6->amount;
                            } else {
                                $result['types'][$o6->pay_type]['packages'] = $o6->amount;
                            }
                            if (isset($result['types'][$o6->pay_type]['journal'])) {
                                $result['types'][$o6->pay_type]['journal'] += $o6->amount;
                            } else {
                                $result['types'][$o6->pay_type]['journal'] = $o6->amount;
                            }
                            if (isset($result['types'][$o6->pay_type]['subtotal'])) {
                                $result['types'][$o6->pay_type]['subtotal'] += $o6->amount;
                            } else {
                                $result['types'][$o6->pay_type]['subtotal'] = $o6->amount;
                            }
                            if (isset($result['total']['packages'])) {
                                $result['total']['packages'] += $o6->amount;//总计
                            } else {
                                $result['total']['packages'] = $o6->amount;
                            }
                            if (isset($result['total']['journal'])) {
                                $result['total']['journal'] += $o6->amount;
                            } else {
                                $result['total']['journal'] = $o6->amount;
                            }
                        }

                        $orders_7 = ReturnServiceCardRecord::LeftJoin('return_service_card_order', 'return_service_card_record.return_order_id', '=', 'return_service_card_order.id')
                            ->LeftJoin('service_card_order', 'service_card_order.order_id', '=', 'return_service_card_order.order_id');
                        if ($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                            $orders_7 = $orders_7->where('service_card_order.shop_id', $shop_id);
                        }
                        $orders_7 = $orders_7->whereBetween('return_service_card_order.implement_time', $time)->where('service_card_order.pay_type', $key)
                            ->select('return_service_card_record.pay_type', 'return_service_card_record.amount')->get();
                        foreach ($orders_7 as $o7) {
                            if (isset($result['types'][$o7->pay_type]['packages'])) {
                                $result['types'][$o7->pay_type]['packages'] -= $o7->amount;
                            } else {
                                $result['types'][$o7->pay_type]['packages'] = -$o7->amount;
                            }
                            if (isset($result['types'][$o7->pay_type]['journal'])) {
                                $result['types'][$o7->pay_type]['journal'] -= $o7->amount;
                            } else {
                                $result['types'][$o7->pay_type]['journal'] = -$o7->amount;
                            }
                            if (isset($result['types'][$o7->pay_type]['subtotal'])) {
                                $result['types'][$o7->pay_type]['subtotal'] -= $o7->amount;
                            } else {
                                $result['types'][$o7->pay_type]['subtotal'] = -$o7->amount;
                            }
                            if (isset($result['total']['packages'])) {
                                $result['total']['packages'] -= $o7->amount;//总计
                            } else {
                                $result['total']['packages'] = -$o7->amount;
                            }
                            if (isset($result['total']['journal'])) {
                                $result['total']['journal'] -= $o7->amount;
                            } else {
                                $result['total']['journal'] = -$o7->amount;
                            }
                        }

                    }else {
                        $result['types'][$key]['pay_type'] = $val;
                        $received_amount = Orders::where('pay_type', $key)->whereNotIn('pay_type', array(6, 8))->whereIn('status', Orders::$complete);
                        if ($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                            $received_amount = $received_amount->where('shop_id', $shop_id);
                        }
                        $received_amount = $received_amount->whereBetween('pay_time', $time)->sum('payable_amount');
                        $received_amount1 = CarWashOrder::where('pay_type', $key)->whereNotIn('pay_type', array(6, 8))->whereIn('status', Orders::$complete);
                        if ($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                            $received_amount1 = $received_amount1->where('shop_id', $shop_id);
                        }
                        $received_amount1 = $received_amount1->whereBetween('pay_time', $time)->sum('pay_amount');
                        $received_amount2 = ScsOperational::getReturnAmount($time, $shop_id, $key);
                       // $result['types'][$key]['received'] = $received_amount + $received_amount1 + $received_amount2;
                        if(isset($result['types'][$key]['received'])){
                            $result['types'][$key]['received'] += $received_amount + $received_amount1 - $received_amount2;
                        }else{
                            $result['types'][$key]['received'] = $received_amount + $received_amount1 - $received_amount2;
                        }
                        if(isset($result['types'][$key]['received_debt'])){
                            $result['types'][$key]['received_debt'] += Receivable::where('verify_status', 1)->where('pay_type', $key)->where('shop_id', $shop_id)->whereBetween('verify_time', $time)->sum('amount');
                        }else{
                            $result['types'][$key]['received_debt'] = Receivable::where('verify_status', 1)->where('pay_type', $key)->where('shop_id', $shop_id)->whereBetween('verify_time', $time)->sum('amount');
                        }
                       // $result['types'][$key]['received_debt'] = Receivable::where('verify_status', 1)->where('pay_type', $key)->where('shop_id', $shop_id)->whereBetween('verify_time', $time)->sum('amount');
                        $packages_amount = ServiceCardOrder::where('pay_type', $key)->whereNotIn('pay_type', array(6, 8))->whereIn('status', Orders::$complete);
                        if ($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                            $packages_amount = $packages_amount->where('shop_id', $shop_id);
                        }
                        $packages_amount = $packages_amount->whereBetween('pay_time', $time)->sum('pay_amount');
                        if(isset($result['types'][$key]['packages'])){
                            $result['types'][$key]['packages'] += $packages_amount;
                        }else{
                            $result['types'][$key]['packages'] = $packages_amount;
                        }


                        $orders_7 = ReturnServiceCardRecord::LeftJoin('return_service_card_order', 'return_service_card_record.return_order_id', '=', 'return_service_card_order.id')
                            ->LeftJoin('service_card_order', 'service_card_order.order_id', '=', 'return_service_card_order.order_id')
                            ->where('return_service_card_record.pay_type', $key)->whereNotIn('return_service_card_record.pay_type', array(6, 8))
                            ->whereBetween('return_service_card_order.implement_time', $time)->where('return_service_card_order.status', 1);
                        if ($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                            $orders_7 = $orders_7->where('service_card_order.shop_id', $shop_id);
                        }
                        $packages_amount = $orders_7->sum('return_service_card_record.amount');
                        if(isset($result['types'][$key]['packages'])){
                            $result['types'][$key]['packages'] -= $packages_amount;
                        }else{
                            $result['types'][$key]['packages'] = -$packages_amount;
                        }
                        //充、撤、退、调
                        $amount_cash = ScsOperational::getShopAccounting(1, $shop_id, $time, $key);
                        $result['types'][$key]['cash_payable'] = $amount_cash;
                        $amount_ins = ScsOperational::getShopAccounting(2, $shop_id, $time, $key);
                        $result['types'][$key]['ins_payable'] = $amount_ins;
                    }
                }elseif($key == 6) {
                    $result['types'][$key]['pay_type'] = '当日应收';
                    $payable_amount = Orders::where('pay_type', $key)->whereIn('status', Orders::$complete);
                    if($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                        $payable_amount = $payable_amount->where('shop_id',$shop_id);
                    }
                    $payable_amount = $payable_amount->whereBetween('pay_time', $time)->sum('payable_amount');
                    $payable_amount1 = CarWashOrder::where('pay_type',$key)->whereIn('status',Orders::$complete);
                    if($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                        $payable_amount1 = $payable_amount1->where('shop_id',$shop_id);
                    }
                    $payable_amount1 = $payable_amount1->whereBetween('pay_time', $time)->sum('pay_amount');
                    //套餐卡单
                    $payable_amount3 = ServiceCardOrder::where('pay_type',$key)->whereIn('status',Orders::$complete);
                    if($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                        $payable_amount3 = $payable_amount3->where('shop_id',$shop_id);
                    }
                    $payable_amount3 = $payable_amount3->whereBetween('pay_time', $time)->sum('pay_amount');
                    if(isset($result['types'][$key]['packages'])){
                        $result['types'][$key]['packages'] += $payable_amount3;
                    }else{
                        $result['types'][$key]['packages'] = $payable_amount3;
                    }
                    $payable_amount3 = ReturnServiceCardRecord::LeftJoin('return_service_card_order', 'return_service_card_record.return_order_id', '=', 'return_service_card_order.id')
                            ->LeftJoin('service_card_order', 'service_card_order.order_id', '=', 'return_service_card_order.order_id')
                            ->where('return_service_card_record.pay_type', $key)
                            ->whereBetween('return_service_card_order.implement_time', $time)->where('return_service_card_order.status', 1);
                        if ($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                            $payable_amount3 = $payable_amount3->where('service_card_order.shop_id', $shop_id);
                        }
                    $payable_amount3 = $payable_amount3->sum('return_service_card_order.amount');
                    if(isset($result['types'][$key]['packages'])){
                        $result['types'][$key]['packages'] -= $payable_amount3;
                    }else{
                        $result['types'][$key]['packages'] = -$payable_amount3;
                    }
                    $payable_amount2 = ScsOperational::getReturnAmount($time,$shop_id,$key);
                    
                    $result['types'][$key]['payable'] = $payable_amount + $payable_amount1 - $payable_amount2;
                }elseif($key == 8){
                    $result['types'][$key]['pay_type'] = '冲减预收';
                    $consume_amount = Orders::where('pay_type', $key)->whereIn('status', Orders::$complete);
                    if($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                        $consume_amount = $consume_amount->where('shop_id',$shop_id);
                    }
                    $consume_amount = $consume_amount->whereBetween('pay_time', $time)->sum('payable_amount');

                    $consume_amount1 = CarWashOrder::where('pay_type',$key)->whereIn('status',Orders::$complete);
                    if($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                        $consume_amount1 = $consume_amount1->where('shop_id',$shop_id);
                    }
                    $consume_amount1 = $consume_amount1->whereBetween('pay_time', $time)->sum('pay_amount');
                    //套餐卡单
                    $consume_amount3 = ServiceCardOrder::where('pay_type',$key)->whereIn('status',Orders::$complete);
                    if($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                        $consume_amount3 = $consume_amount3->where('shop_id',$shop_id);
                    }
                    $consume_amount3 = $consume_amount3->whereBetween('pay_time', $time)->sum('pay_amount');
                    $consume_amount2 = ScsOperational::getReturnAmount($time,$shop_id,$key);
                    if(isset($result['types'][$key]['packages'])){
                        $result['types'][$key]['packages'] += $consume_amount3;
                    }else{
                        $result['types'][$key]['packages'] = $consume_amount3;
                    }

                    $payable_amount3 = ReturnServiceCardRecord::LeftJoin('return_service_card_order', 'return_service_card_record.return_order_id', '=', 'return_service_card_order.id')
                            ->LeftJoin('service_card_order', 'service_card_order.order_id', '=', 'return_service_card_order.order_id')
                            ->where('return_service_card_record.pay_type', $key)
                            ->whereBetween('return_service_card_order.implement_time', $time)->where('return_service_card_order.status', 1);
                        if ($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                            $payable_amount3 = $payable_amount3->where('service_card_order.shop_id', $shop_id);
                        }
                    $payable_amount3 = $payable_amount3->sum('return_service_card_order.amount');
                    if(isset($result['types'][$key]['packages'])){
                        $result['types'][$key]['packages'] -= $payable_amount3;
                    }else{
                        $result['types'][$key]['packages'] = -$payable_amount3;
                    }
                    
                    $result['types'][$key]['consume'] = $consume_amount + $consume_amount1 - $consume_amount2;
                    $result['types'][$key]['received_debt'] = Receivable::where('verify_status',1)->where('pay_type',$key)->where('shop_id',$shop_id)->whereBetween('verify_time', $time)->sum('amount');
                }
                $te = (isset($result['types'][$key]['received']) ? $result['types'][$key]['received'] : 0)
                + (isset($result['types'][$key]['payable']) ? $result['types'][$key]['payable'] : 0)
                + (isset($result['types'][$key]['consume']) ? $result['types'][$key]['consume'] : 0)
                + (isset($result['types'][$key]['packages']) ? $result['types'][$key]['packages'] : 0);
                if(isset($result['types'][$key]['journal'])){
                    $result['types'][$key]['journal'] += $te;
                }else{
                    $result['types'][$key]['journal'] = $te;
                }
                $te1 = $result['types'][$key]['journal']
                    + (isset($result['types'][$key]['received_debt']) ? $result['types'][$key]['received_debt'] : 0)
                    + (isset($result['types'][$key]['cash_payable']) ? $result['types'][$key]['cash_payable'] : 0)
                    + (isset($result['types'][$key]['ins_payable']) ? $result['types'][$key]['ins_payable'] : 0);
                if(isset($result['types'][$key]['subtotal'])){
                    $result['types'][$key]['subtotal'] += $te1;
                }else{
                    $result['types'][$key]['subtotal'] = $te1;
                }
                if(isset($result['total']['received'])) {
                    $result['total']['received'] += (isset($result['types'][$key]['received']) ? $result['types'][$key]['received'] : 0);//总计
                }else{
                    $result['total']['received'] = (isset($result['types'][$key]['received']) ? $result['types'][$key]['received'] : 0);
                }
                if(isset($result['total']['payable'])) {
                    $result['total']['payable'] += (isset($result['types'][$key]['payable']) ? $result['types'][$key]['payable'] : 0);//总计
                }else{
                    $result['total']['payable'] = (isset($result['types'][$key]['payable']) ? $result['types'][$key]['payable'] : 0);
                }
                if(isset($result['total']['consume'])) {
                    $result['total']['consume'] += (isset($result['types'][$key]['consume']) ? $result['types'][$key]['consume'] : 0);//总计
                }else{
                    $result['total']['consume'] = (isset($result['types'][$key]['consume']) ? $result['types'][$key]['consume'] : 0);
                }
                if(isset($result['total']['packages'])) {
                    $result['total']['packages'] += (isset($result['types'][$key]['packages']) ? $result['types'][$key]['packages'] : 0);//总计
                }else{
                    $result['total']['packages'] = (isset($result['types'][$key]['packages']) ? $result['types'][$key]['packages'] : 0);
                }
                if(isset($result['total']['journal'])) {
                    $result['total']['journal'] += $result['types'][$key]['journal'];//总计
                }else{
                    $result['total']['journal'] = $result['types'][$key]['journal'];
                }
                if(isset($result['total']['received_debt'])) {
                    $result['total']['received_debt'] += (isset($result['types'][$key]['received_debt']) ? $result['types'][$key]['received_debt'] : 0);//总计
                }else{
                    $result['total']['received_debt'] = (isset($result['types'][$key]['received_debt']) ? $result['types'][$key]['received_debt'] : 0);
                }
                if(isset($result['total']['cash_payable'])) {
                    $result['total']['cash_payable'] += (isset($result['types'][$key]['cash_payable']) ? $result['types'][$key]['cash_payable'] : 0);//总计
                }else{
                    $result['total']['cash_payable'] = (isset($result['types'][$key]['cash_payable']) ? $result['types'][$key]['cash_payable'] : 0);
                }
                if(isset($result['total']['ins_payable'])) {
                    $result['total']['ins_payable'] += (isset($result['types'][$key]['ins_payable']) ? $result['types'][$key]['ins_payable'] : 0);//总计
                }else{
                    $result['total']['ins_payable'] = (isset($result['types'][$key]['ins_payable']) ? $result['types'][$key]['ins_payable'] : 0);
                }
            }
            $pay_type[] = "废品收入";
            $pay_type[] = "现金账户预收";
            $pay_type[] = "虚拟账户预收";
            $pay_type[] = "外部调出";
            $scrap_num = array_search("废品收入",$pay_type);
            $external_num = array_search("外部调出",$pay_type);
            $cash_num = array_search("现金账户预收",$pay_type);
            $ins_num = array_search("虚拟账户预收",$pay_type);
            $scrap_a = ShopScrap::where('collect_date',$date);
            if($shop_id !== '' && $shop_id != '-1' && $shop_id !== '0') {
                $scrap_a = $scrap_a->where('shop_id',$shop_id);
            }
            $scrap_a = $scrap_a->sum('amount');
            $external_a = ScsOperational::getOutListCost($shop_id,$date,$date);
            $cash_a = ScsOperational::getAccountPay($shop_id,$time,1);
            $ins_a = ScsOperational::getAccountPay($shop_id,$time,2);
            $result['types'][$scrap_num]['pay_type'] = "废品收入";
            $result['types'][$external_num]['pay_type'] = "外部调出";
            $result['types'][$cash_num]['pay_type'] = "现金账户预收";
            $result['types'][$ins_num]['pay_type'] = "虚拟账户预收";
            $result['types'][$scrap_num]['scrap'] = $scrap_a;
            $result['types'][$external_num]['external'] = $external_a;
            $result['types'][$scrap_num]['subtotal'] = $scrap_a;
            $result['types'][$external_num]['subtotal'] = $external_a;
            $result['types'][$cash_num]['consume'] = $cash_a;
            $result['types'][$ins_num]['consume'] = $ins_a;
            $result['types'][$cash_num]['journal'] = $cash_a;
            $result['types'][$ins_num]['journal'] = $ins_a;
            $result['types'][$cash_num]['subtotal'] = $cash_a;
            $result['types'][$ins_num]['subtotal'] = $ins_a;
            if(isset($result['total']['scrap'])) {
                $result['total']['scrap'] += $result['types'][$scrap_num]['scrap'];//总计
            }else{
                $result['total']['scrap'] = $result['types'][$scrap_num]['scrap'];
            }
            if(isset($result['total']['external'])) {
                $result['total']['external'] += $result['types'][$external_num]['external'];//总计
            }else{
                $result['total']['external'] = $result['types'][$external_num]['external'];
            }
            foreach($result['types'] as $key=>&$type){
                foreach($cols as $col){
                    if(!isset($type[$col])){
                        $type[$col] = "/";
                    }
                }
                foreach($type as &$va){
                    if(is_numeric($va)){
                        $va = $va / 100;
                    }
                }
                if(!isset($type['pay_type'])){
                    unset($result['types'][$key]);
                }
            }
            $result['total']['subtotal'] = $result['total']['journal'] + $result['total']['scrap'] + $result['total']['received_debt'] + $result['total']['cash_payable'] + $result['total']['ins_payable'] + $result['total']['external'];//总计
            foreach($result['total'] as &$v1){
                $v1 = $v1 / 100;
            }
            $response['result'] = $result;
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getDailyData", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}